import { HeroService } from './../hero.service';
import { Component, OnInit } from '@angular/core';
import { Hero } from '../hero';

@Component({
  selector: 'app-heroes',
  templateUrl: './heroes.component.html',
  styleUrls: ['./heroes.component.css']
})
export class HeroesComponent implements OnInit {
  heroes: Hero[];//数据源
  // selectedHero: Hero;//选定数据

  ngOnInit(): void {
    this.getHeroes();
  }

  //默认对象
  hero: Hero = {
    id: 1,
    name: 'Windstorm'
  }
  //选定传值
  // onSelect(hero: Hero): void {
  //   this.selectedHero = hero;
  // }
  //动态注入
  constructor(
    private heroService: HeroService
  ) {

   }
  //绑定数据
  public getHeroes(): void {
    this.heroService.getHeroes()
    .subscribe(heroes => this.heroes = heroes);
  }
  add(name: string): void {
    name = name.trim();
    if (!name) {
      return;
    }
  }
  
}
